import allure
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.support import expected_conditions

from tests.app.pages.complete_input_page import CompleteInputPage
from tests.app.pages.wecom import Wecom

@allure.feature("添加成员页面")
class QuickAddMembersPage(Wecom):
    __NAME=(AppiumBy.XPATH,"//*[@resource-id='com.tencent.wework:id/hll']/child::*/child::*[3]")
    __PHONE=(AppiumBy.ID,"com.tencent.wework:id/j6_")
    __MAIL=(AppiumBy.XPATH,"//*[@resource-id='com.tencent.wework:id/hkv']/child::*/child::*[2]")
    __PRESERVATION=(AppiumBy.ID,"com.tencent.wework:id/b60")
    __RETURN_BUTTON=(AppiumBy.ID,"com.tencent.wework:id/mh0")
    __ERR0R_NAME=(AppiumBy.ID,"com.tencent.wework:id/cy9")
    __SPECIAL=(AppiumBy.XPATH,"//*[@class='android.widget.Toast' and @index='1']")
    __ERR0R=(AppiumBy.XPATH,"//*[@text='姓名不能为空']")
    __EMAIL_ERROR=(AppiumBy.XPATH,"//*[@text='邮箱格式不正确']")
    __PHONE_ERROR=(AppiumBy.XPATH,"//*[@text='请填写合法的手机号']")
    __COMPLETE_INPUT=(AppiumBy.XPATH,"//*[@text='完整输入']")
    __QUICK_BUTTON=(AppiumBy.XPATH,"//*[@text='快速输入']")
    @allure.story("跳转完整输入页面")
    def goto_complete_input(self):
        try:
            self.find(*self.__COMPLETE_INPUT)
            self.find_and_click(*self.__COMPLETE_INPUT)
            return CompleteInputPage(self.driver)
        except :
            return CompleteInputPage(self.driver)

    @allure.story("点击快速输入按钮")
    def quick_input_button(self):
        try:
            self.find(*self.__QUICK_BUTTON)
            self.find_and_click(*self.__QUICK_BUTTON)
            return self
        except :
            return self

    @allure.story("[添加成功]成员一")
    def add_first_member(self,name,phone,mail):
        from tests.app.pages.address_book_page import AddressBookPage
        with allure.step("输入姓名"):
            self.find_and_send(*self.__NAME,text=f"{name}")
        with allure.step("输入手机号"):
            self.find_and_send(*self.__PHONE,text=f"{phone}")
        with allure.step("输入邮箱"):
            self.find_and_send(*self.__MAIL,text=f"{mail}")
        with allure.step("点击保存"):
            self.find_and_click(*self.__PRESERVATION)
        self.wait(method=expected_conditions.visibility_of_element_located((self.__RETURN_BUTTON)))
        with allure.step("点击返回"):
            self.find_and_click(*self.__RETURN_BUTTON)
        return AddressBookPage(self.driver)

    @allure.story("[添加成功]成员二")
    def add_second_member(self,name,phone):
        from tests.app.pages.address_book_page import AddressBookPage
        with allure.step("输入姓名"):
            self.find_and_send(*self.__NAME, text=f"{name}")
        with allure.step("输入手机号"):
            self.find_and_send(*self.__PHONE, text=f"{phone}")
        with allure.step("点击保存"):
            self.find_and_click(*self.__PRESERVATION)
        self.wait(method=expected_conditions.visibility_of_element_located((self.__RETURN_BUTTON)))
        with allure.step("点击返回"):
            self.find_and_click(*self.__RETURN_BUTTON)
        return AddressBookPage(self.driver)

    @allure.story("[添加成功]成员三")
    def add_third_member(self,name,mail):
        from tests.app.pages.address_book_page import AddressBookPage
        with allure.step("输入姓名"):
            self.find_and_send(*self.__NAME, text=f"{name}")
        with allure.step("输入邮箱"):
            self.find_and_send(*self.__MAIL, text=f"{mail}")
        with allure.step("点击保存"):
            self.find_and_click(*self.__PRESERVATION)
        self.wait(method=expected_conditions.visibility_of_element_located((self.__RETURN_BUTTON)))
        with allure.step("点击返回"):
            self.find_and_click(*self.__RETURN_BUTTON)
        return AddressBookPage(self.driver)

    @allure.story("[添加失败]仅输入姓名")
    def add_only_name(self,name):
        with allure.step("输入姓名"):
            self.find_and_send(*self.__NAME, text=f"{name}")
        with allure.step("点击保存"):
            self.find_and_click(*self.__PRESERVATION)
        self.wait(method=expected_conditions.visibility_of_element_located((self.__ERR0R_NAME)))
        ele=self.find(*self.__ERR0R_NAME)
        return ele.text

    @allure.story("[添加失败]姓名添加特殊字符")
    def add_special_characters(self,name):
        with allure.step("在姓名输入特殊字符"):
            self.find_and_send(*self.__NAME, text=f"{name}")
        ele =self.find(*self.__SPECIAL)
        return ele.text

    @allure.story("[添加失败]仅输入手机号")
    def add_only_phone(self,phone):
        with allure.step("输入手机号"):
            self.find_and_send(*self.__PHONE, text=f"{phone}")
        with allure.step("点击保存"):
            self.find_and_click(*self.__PRESERVATION)
        self.wait(method=expected_conditions.visibility_of_element_located((self.__ERR0R)))
        ele = self.find(*self.__ERR0R)
        return ele.text

    @allure.story("[添加失败]仅输入邮箱")
    def add_only_mail(self,mail):
        with allure.step("输入邮箱"):
            self.find_and_send(*self.__MAIL, text=f"{mail}")
        with allure.step("点击保存"):
            self.find_and_click(*self.__PRESERVATION)
        self.wait(method=expected_conditions.visibility_of_element_located((self.__ERR0R)))
        ele = self.find(*self.__ERR0R)
        return ele.text

    @allure.story("[添加失败]输入手机号和邮箱")
    def add_phone_mail(self,phone,mail):
        self.find_and_send(*self.__PHONE, text=f"{phone}")
        self.find_and_send(*self.__MAIL, text=f"{mail}")
        self.find_and_click(*self.__PRESERVATION)
        self.wait(method=expected_conditions.visibility_of_element_located((self.__ERR0R)))
        ele = self.find(*self.__ERR0R)
        return ele.text

    @allure.story("[添加失败]邮箱格式错误")
    def add_error_mail(self,name,mail):
        self.find_and_send(*self.__NAME, text=f"{name}")
        self.find_and_send(*self.__MAIL, text=f"{mail}")
        self.find_and_click(*self.__PRESERVATION)
        self.wait(method=expected_conditions.visibility_of_element_located((self.__EMAIL_ERROR)))
        ele =self.find(*self.__EMAIL_ERROR)
        return ele.text

    @allure.story("[添加失败]手机格式错误")
    def add_error_phone(self,name,phone):
        self.find_and_send(*self.__NAME, text=f"{name}")
        self.find_and_send(*self.__PHONE, text=f"{phone}")
        self.find_and_click(*self.__PRESERVATION)
        self.wait(method=expected_conditions.visibility_of_element_located((self.__PHONE_ERROR)))
        ele = self.find(*self.__PHONE_ERROR)
        return ele.text
